package com.hanter.rad.widget.cupertino.utils;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.hanter.pickerview.utils.R;
import com.hanter.rad.widget.cupertino.PickerView;

/**
 * 类名：PickerFragment <br/>
 * 描述：Picker选择的Fragment
 * 创建时间：2019/10/13 14:08
 *
 * @author hanter
 * @version 1.0
 */
public class BasePickerFragment extends Fragment implements View.OnClickListener {

    public static final String TAG = "PickerDialog";

    public static final int ANIMATION_DURATION = 200;

    protected PickerView pvOptions;
    protected View contentView;
    protected View pickerContainer;
    private boolean dismissed;
    private ColorDrawable colorDrawable;

    @Nullable
    protected PickerProvider provider;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.picker_view_container, container, false);
        initViews();
        return contentView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    private void initViews() {
        contentView.setOnClickListener(this);

        TextView tvTitle = contentView.findViewById(R.id.tvTitle);
        Button btnSubmit = contentView.findViewById(R.id.btnSubmit);
        Button btnCancel = contentView.findViewById(R.id.btnCancel);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        pvOptions = contentView.findViewById(R.id.pvOptions);
        if (provider != null) {
            pvOptions.setDataSource(provider.getDataSource(getTag()));
            pvOptions.setPickerDelegate(provider.getDelegate(getTag()));
        }

        initAnimations();
    }

    private void initAnimations() {
        colorDrawable = (ColorDrawable) contentView.getBackground();
        ValueAnimator animator = ValueAnimator.ofInt(0, 153);
        animator.setDuration(ANIMATION_DURATION);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                colorDrawable.setColor(Color.argb((Integer) animation.getAnimatedValue(), 0, 0, 0));
            }
        });
        animator.start();

        pickerContainer = contentView.findViewById(R.id.picker_container);
        pickerContainer.startAnimation(AnimationUtils.loadAnimation(contentView.getContext(), R.anim.pickerview_slide_in_bottom));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.picker_root_container) {
            dismiss();
        }
    }

    public void dismiss() {
        if (dismissed)
            return;

        this.dismissed = true;

        endAnimation();
    }

    private void endAnimation() {
        ValueAnimator animator = ValueAnimator.ofInt(153, 0);
        animator.setDuration(ANIMATION_DURATION);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                colorDrawable.setColor(Color.argb((Integer) animation.getAnimatedValue(), 0, 0, 0));
            }
        });
        animator.start();

        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.pickerview_slide_out_bottom);
        animation.setDuration(ANIMATION_DURATION);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (getFragmentManager() != null) {
                    getFragmentManager().popBackStackImmediate();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        pickerContainer.startAnimation(animation);
    }

    public void show(FragmentManager manager) {
        show(manager, null);
    }

    public void show(FragmentManager manager, String stackName) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(android.R.id.content, this, stackName)
                .addToBackStack(stackName);
        ft.commit();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
